package com.bleeth; /**
 * Copyright (c) 2008, 2012 Oracle and/or its affiliates.
 * All rights reserved. Use is subject to license terms.
 */

import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.animation.TimelineBuilder;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.effect.Lighting;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 * A sample that shows various types of interpolation between key frames in a
 * timeline. There are five circles, each animated with a different
 * interpolation method. The Linear interpolator is the default. Use the
 * controls to reduce opacity to zero for some circles to compare with others,
 * or change circle color to distinguish between individual interpolators.
 *
 * @see javafx.animation.Interpolator
 * @see javafx.animation.KeyFrame
 * @see javafx.animation.KeyValue
 * @see javafx.animation.Timeline
 * @see javafx.util.Duration
 */
public class InterpolatorSample extends Application {
    private Timeline timeline = new Timeline();
    private Double x1Val, y1Val, x2Val, y2Val;
    private Circle circle1;
    private Circle circle2;
    private Circle circle3;
    private Circle circle4;
    private Circle circle5;

    private void init(Stage primaryStage) {
        Group root = new Group();
        primaryStage.setResizable(false);
        primaryStage.setScene(new Scene(root, 200, 200));
        x1Val = 0.5;
        y1Val = 0.1;
        x2Val = 0.5;
        y2Val = 0.1;

//create circles by method createMovingCircle listed below
        circle1 = createMovingCircle(Interpolator.LINEAR, Color.RED); //default interpolator
        circle1.setOpacity(0.7);

        circle2 = createMovingCircle(Interpolator.EASE_BOTH, Color.VIOLET); //circle slows down when reached both ends of trajectory
        circle2.setOpacity(0.45);
        circle2.setCenterY(60);

        circle3 = createMovingCircle(Interpolator.EASE_IN, Color.BLUE);
        circle3.setOpacity(0.20);
        circle3.setCenterY(95);

        circle4 = createMovingCircle(Interpolator.EASE_OUT, Color.YELLOW);
        circle4.setOpacity(0.35);
        circle4.setCenterY(130);

        circle5 = createMovingCircle(Interpolator.SPLINE(x1Val, y1Val, x2Val, y2Val), Color.GREEN); //one can define own behaviour of interpolator by spline method
        circle5.setOpacity(0.7);
        circle5.setCenterY(165);

        root.getChildren().addAll(
                circle1,
                circle2,
                circle3,
                circle4,
                circle5
        );

//Slider for SPLINE//
        Slider x1 = new Slider(0.0, 1.0, 0.5);
        x1.setValue(x1Val);
        x1.valueProperty().addListener(new ChangeListener() {
            @Override
            public void changed(ObservableValue o, Object oldVal, Object newVal) {
                x1Val = (Double) newVal;
                changeInterpolator(Interpolator.SPLINE(x1Val, y1Val, x2Val, y2Val));

            }

        });
        Slider y1 = new Slider(0.0, 1.0, 0.5);
        y1.setValue(y1Val);
        y1.valueProperty().addListener(new ChangeListener() {
            @Override
            public void changed(ObservableValue o, Object oldVal, Object newVal) {
                y1Val = (Double) newVal;
                changeInterpolator(Interpolator.SPLINE(x1Val, y1Val, x2Val, y2Val));

            }

        });
        Slider x2 = new Slider(0.0, 1.0, 0.5);
        x2.setValue(x2Val);
        x2.valueProperty().addListener(new ChangeListener() {
            @Override
            public void changed(ObservableValue o, Object oldVal, Object newVal) {
                x2Val = (Double) newVal;
                changeInterpolator(Interpolator.SPLINE(x1Val, y1Val, x2Val, y2Val));

            }

        });
        Slider y2 = new Slider(0.0, 1.0, 0.5);
        y2.setValue(y2Val);

        y2.valueProperty().addListener(new ChangeListener() {
            @Override
            public void changed(ObservableValue o, Object oldVal, Object newVal) {
                y2Val = (Double) newVal;
                changeInterpolator(Interpolator.SPLINE(x1Val, y1Val, x2Val, y2Val));

            }

        });
    }


    private Circle createMovingCircle(Interpolator interpolator, Color color) {
        Circle circle = new Circle(25, 25, 35, color);
        circle.setOpacity(0.0);
//add effect
        circle.setEffect(new Lighting());

//create a timeline for moving the circle

        timeline.setCycleCount(Timeline.INDEFINITE);
        timeline.setAutoReverse(true);

//create a keyValue for horizontal translation of circle to the position 155px with given interpolator
        KeyValue keyValue = new KeyValue(circle.translateXProperty(), 155, interpolator);

//create a keyFrame with duration 4s
        KeyFrame keyFrame = new KeyFrame(Duration.seconds(4), keyValue);

//add the keyframe to the timeline
        timeline.getKeyFrames().add(keyFrame);

        return circle;
    }


    public void changeInterpolator(Interpolator newinterpolator) {
        Duration currenttime = Duration.ZERO;
        if (timeline != null) {
            currenttime = timeline.getCurrentTime();

            timeline.stop();
        }
        timeline = TimelineBuilder.create()
                .cycleCount(Timeline.INDEFINITE)
                .autoReverse(true)
                .keyFrames(
                        new KeyFrame(
                                Duration.ZERO,
                                new KeyValue(circle1.translateXProperty(), 0, Interpolator.LINEAR),
                                new KeyValue(circle2.translateXProperty(), 0, Interpolator.EASE_BOTH),
                                new KeyValue(circle3.translateXProperty(), 0, Interpolator.EASE_IN),
                                new KeyValue(circle4.translateXProperty(), 0, Interpolator.EASE_OUT),
                                new KeyValue(circle5.translateXProperty(), 0, newinterpolator)
                        ),
                        new KeyFrame(
                                Duration.seconds(4),
                                new KeyValue(circle1.translateXProperty(), 155, Interpolator.LINEAR),
                                new KeyValue(circle2.translateXProperty(), 155, Interpolator.EASE_BOTH),
                                new KeyValue(circle3.translateXProperty(), 155, Interpolator.EASE_IN),
                                new KeyValue(circle4.translateXProperty(), 155, Interpolator.EASE_OUT),
                                new KeyValue(circle5.translateXProperty(), 155, newinterpolator)
                        )
                )
                .build();

        timeline.playFrom(currenttime);


    }


    public void play() {
        timeline.play();
    }

    @Override
    public void stop() {
        timeline.stop();
    }

    public double getSampleWidth() {
        return 200;
    }

    public double getSampleHeight() {
        return 200;
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        init(primaryStage);
        primaryStage.show();
        play();
    }

    public static void main(String[] args) {
        launch(args);
    }
}